package bcc.java8.chap01.exe01;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * .
 *
 * @author: bcc
 * 2020/10/29
 */
public class SortTest {
    public static void main(String[] args) {
        List<Apple> inventory = Arrays.asList(new Apple(80,"green"),
                new Apple(155, "green"),
                new Apple(120, "red"));

        // java 8之前的方式
        Collections.sort(inventory, new Comparator<Apple>() {
            @Override
            public int compare(final Apple o1, final Apple o2) {
                return o1.getWeight().compareTo(o2.getWeight());
            }
        });
        System.out.println(inventory);

        //java8排序
        List<Apple> inventory2 = Arrays.asList(new Apple(80,"green"),
                new Apple(155, "green"),
                new Apple(120, "red"));
        inventory2.sort(Comparator.comparing(Apple::getWeight));
        System.out.println(inventory2);

        //java8排序 倒序
        List<Apple> inventory3 = Arrays.asList(new Apple(80,"green"),
                new Apple(155, "green"),
                new Apple(120, "red"));
        inventory3.sort(Comparator.comparing(Apple::getWeight).reversed());
        System.out.println(inventory3);
    }
}
